package com.neusoft.controller;

import com.neusoft.po.CommonResult;
import com.neusoft.po.Comment;
import com.neusoft.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/CommentController")
@RefreshScope
public class CommentController {  

    @Autowired 
    private CommentService commentService;

    @PostMapping("/createComment/{userId}/{businessId}/{comment}/{rank}")
    public CommonResult<Boolean> createComment(
            @PathVariable("userId") String userId,
            @PathVariable("businessId") Integer businessId,
            @PathVariable("comment") String comment,
            @PathVariable("rank") Integer rank) {
        Comment comment1 = new Comment();
        comment1.setUserId(userId);
        comment1.setBusinessId(businessId);
        comment1.setComment(comment);
        comment1.setRank(rank);
        Boolean result = commentService.createComment(userId, businessId, comment, rank);
        return new CommonResult<>(200, "Comment created successfully", result);
    }

    @GetMapping("/getCommentsByBusinessId/{businessId}")
    public CommonResult<List<Comment>> getCommentsByBusinessId(
            @PathVariable("businessId") Integer businessId) {
        List<Comment> comments = commentService.getCommentByBusinessId(businessId);
        return new CommonResult<>(200, "Comments retrieved successfully", comments);
    }

    @GetMapping("/getCommentsByUserId/{userId}")
    public CommonResult<List<Comment>> getCommentsByUserId(
            @PathVariable("userId") String userId) {
        List<Comment> comments = commentService.getCommentByUserId(userId);
        return new CommonResult<>(200, "Comments retrieved successfully", comments);
    }

    @PutMapping("/updateComment/{commentId}/{comment}/{rank}")
    public CommonResult<Boolean> updateComment(
            @PathVariable("commentId") Integer commentId,
            @PathVariable("comment") String comment,
            @PathVariable("rank") Integer rank) {
        Boolean result = commentService.updateComment(commentId, comment, rank);
        return new CommonResult<>(200, "Comment updated successfully", result);
    }
}